const mysql = require('mysql2/promise');

async function checkHierarchy () {
  const connection = await mysql.createConnection({
    host: '43.143.253.188',
    port: 3306,
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs',
    timezone: '+08:00'
  });

  try {
    console.log('🔍 检查地图区域层级关系...\n');

    // 检查有父级关系的区域
    const [parentAreas] = await connection.execute(`
            SELECT 
                \`id\`, 
                \`name\`, 
                \`parent_id\`, 
                \`extension\`
            FROM \`map_areas\` 
            WHERE \`parent_id\` IS NOT NULL
            ORDER BY \`parent_id\`, \`id\`
        `);

    console.log('📊 有父级关系的区域:');
    if (parentAreas.length > 0) {
      parentAreas.forEach(area => {
        const name = area.extension ? `${area.name}${area.extension}` : area.name;
        console.log(`  ID: ${area.id.toString().padStart(4)} | ${name.padEnd(20)} | 父级ID: ${area.parent_id}`);
      });
    } else {
      console.log('  暂无有父级关系的区域');
    }

    // 检查顶级区域（没有父级的区域）
    const [topLevelAreas] = await connection.execute(`
            SELECT 
                \`id\`, 
                \`name\`, 
                \`parent_id\`, 
                \`extension\`
            FROM \`map_areas\` 
            WHERE \`parent_id\` IS NULL
            ORDER BY \`id\`
            LIMIT 10
        `);

    console.log('\n📊 顶级区域示例:');
    topLevelAreas.forEach(area => {
      const name = area.extension ? `${area.name}${area.extension}` : area.name;
      console.log(`  ID: ${area.id.toString().padStart(4)} | ${name.padEnd(20)} | 顶级区域`);
    });

    // 检查有等级信息的区域
    const [levelAreas] = await connection.execute(`
            SELECT 
                \`id\`, 
                \`name\`, 
                \`parent_id\`, 
                \`extension\`
            FROM \`map_areas\` 
            WHERE \`extension\` IS NOT NULL
            ORDER BY \`id\`
        `);

    console.log('\n📊 有等级信息的区域:');
    if (levelAreas.length > 0) {
      levelAreas.forEach(area => {
        const name = area.extension ? `${area.name}${area.extension}` : area.name;
        console.log(`  ID: ${area.id.toString().padStart(4)} | ${name.padEnd(20)} | 父级ID: ${area.parent_id || 'NULL'}`);
      });
    } else {
      console.log('  暂无有等级信息的区域');
    }

    // 统计信息
    const [stats] = await connection.execute(`
            SELECT 
                COUNT(*) as total_areas,
                COUNT(parent_id) as areas_with_parent,
                COUNT(CASE WHEN parent_id IS NULL THEN 1 END) as top_level_areas,
                COUNT(extension) as areas_with_extension
            FROM map_areas
        `);

    console.log('\n📊 统计信息:');
    console.log(`  总区域数: ${stats[0].total_areas}`);
    console.log(`  有父级区域数: ${stats[0].areas_with_parent}`);
    console.log(`  顶级区域数: ${stats[0].top_level_areas}`);
    console.log(`  有等级信息区域数: ${stats[0].areas_with_extension}`);

  } catch (error) {
    console.error('❌ 检查失败:', error.message);
  } finally {
    await connection.end();
    console.log('\n🔌 数据库连接已关闭');
  }
}

checkHierarchy(); 